﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management;
using System.Data.SqlClient;

namespace DBCodeGenerator
{
    public class MSSQLInfomation
    {
        public MSSQLInfomation()
        {

        }


        public DataTable GetTableList(string dbname)
        {
            SqlConnection conn = new SqlConnection(GlobalSettings.ConnectionString + "Database=" + dbname);
            DataTable tbl = new DataTable();
            try
            {
                conn.Open();
                tbl = conn.GetSchema("Tables", new string[] { null, null, null, "BASE TABLE" });
            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                if (conn != null && conn.State == ConnectionState.Open)
                    conn.Close();
            }

            return tbl;
        }

        public DataTable GetTableinfo(string tablename,string database,string schema)
        {
            SqlConnection conn = new SqlConnection(GlobalSettings.ConnectionString + "Database=" + database);
            DataTable tbl = new DataTable();
            string sqluncmd = "Select * from {0}.INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = '{1}' and TABLE_NAME = '{2}'";
            try
            {
                string sqlcmd = String.Format(sqluncmd, database, schema, tablename);
                conn.Open();
                SqlDataAdapter adp = new SqlDataAdapter(sqlcmd, conn);
                adp.Fill(tbl);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (conn != null && conn.State == ConnectionState.Open)
                    conn.Close();
            }

            return tbl;
        }

       

    }
}
